<template>
  <!-- 左侧菜单 -->
  <div>
    <van-cell v-for="(value,key,index) in order" v-bind:key="index" :title="value.name" :value="value.count"
              :label="value.price" @click="test(key)"/>


    <van-submit-bar ref="bar"
                    button-text="提交订单"
                    :price="getTotalPrice"
                    @click="sumbit"
    >
    </van-submit-bar>


<!--    -->
    <van-dialog id="dialog-sumbit" use-slot>
      <van-field
        @input="bingPrice"
        :value="price"
        label="折后价"
        placeholder="请输入打折后的价格"
        required
        border="false"
      />
    </van-dialog>
  </div>
</template>

<script>
  import Dialog from '../../../static/vant/dialog/Dialog';
  import Toast from '../../../static/vant//toast/Toast';
  import {mapState, mapMutations, mapActions, mapGetters} from 'vuex'
  export default {
    data() {
      return {
        order: {},
        price:this.getTotalPrice
      }
    },
    created() {

      // this.computedPrice = this.totalPrice;
    },
    mounted() {
      this.order = this.getOrder;
      console.log(this.getOrder);
      // this.order.
      console.log(this.getTotalPrice)

    },
    computed: {
      totalPrice() {
        return 125462
      },
      // ...mapState(['order']),
      ...mapGetters(['getOrder','getTotalPrice']),
      ...mapGetters(['getHasUserInfo','getStoreUserInfo','getTotalPrice',"getDesk"])
    },
    methods: {
      ...mapMutations(['addOrder', 'subOrder']),
      sumbit() {
        /*
        *  提交：
        *
        * */
        const db = wx.cloud.database({env: 'jkoala-98c74'});
        Dialog.confirm({
          // context:'#message',
          selector: "#dialog-sumbit",
          title: '确认价格',
        })
          .then(() => {
            //点击确认
            //1.0点击确认  提交订单
            let order = this.getOrder;
            order["createDate"] = db.serverDate();
            order["name"] = this.getStoreUserInfo.nickName;
            order['totalPrice'] = this.price;
            db.collection("orders").add({
              data:order
            })
              .then(res => {
                console.log(res)
              })
              .catch(console.error)

            //2.0初始化桌子参数
            db.collection("desks").where({
              _id:this.getDesk
            }).update({
              data:{
                num:0,
                //非多人点所以不需要
                order:null,
                //不可用
                state:2,
              }
            }).then()
            setTimeout(res=>{
              wx.redirectTo({
                url:'../pay/main'
              })
            },1000)
          })
          .catch(() => {
            // on cancel
            console.log("cancel")

          })
          //提交后进入 pay 界面

        // db.collection("desks").doc(this.getDesk).get().then(res=>{
        //     if(res.state === 2){
        //       Toast("订单已提交")
        //       return ;
        //     }
        // });
        // let order = this.getOrder;
        // order["createDate"] = db.serverDate();
        // order["name"] = this.getStoreUserInfo.nickName;
        // order['totalPrice'] = this.getTotalPrice;
        // db.collection("orders").add({
        //   data:order
        // })
        // .then(res => {
        //   console.log(res)
        // })
        // .catch(console.error)
      },
      bingPrice(e) {
        this.price = e.mp.detail
      },
    }
  }
</script>

<style>

</style>
